package com.pablosz.worm.components;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.pablosz.worm.Lifecicle;
import com.pablosz.worm.LifecicleComponent;
import com.pablosz.worm.util.LifecicleComponents;


public class Screen extends LifecicleComponent
{
	private transient Paint paint;
	
	private int squareSize;
	private int backgroundColor=Color.WHITE;
	
	public Screen()
	{
		this.paint = new Paint();
		paint.setColor(Color.GRAY);
	}
	
	@Override
	public void init(Lifecicle lf,LifecicleComponents components,long width,long height)
	{
	}
	
	@Override
	public void print(Canvas canvas)
	{
		dibujarLineas(canvas);
	}

	@Override
	public void process(Lifecicle lf,LifecicleComponents components,long width,long height)
	{
	}
	
	@Override
	public boolean finish(Lifecicle lf,LifecicleComponents  components,long width,long height)
	{
		return true;
	}

	private void dibujarLineas(Canvas canvas)
	{
		
		canvas.drawColor(backgroundColor);
		
		float width=canvas.getWidth();
		float height=canvas.getHeight();
		
		float xCentro=width/2;
		float yCentro=height/2;
		
		// dibujo lineas horizontales
		float filaDibArr=yCentro-squareSize/2;
		float filaDibAba=yCentro+squareSize/2;
		while(filaDibArr>=0)
		{
			canvas.drawLine(0, filaDibArr, width, filaDibArr, paint);
			canvas.drawLine(0, filaDibAba, width, filaDibAba, paint);
			
			filaDibArr-=2*squareSize/2;
			filaDibAba+=2*squareSize/2;
		}		
		
		// dibujo lineas horizontales
		float colDibIzq=xCentro-squareSize/2;
		float colDibDer=xCentro+squareSize/2;
		while(colDibIzq>=0)
		{
			canvas.drawLine(colDibIzq, 0, colDibIzq, height, paint);
			canvas.drawLine(colDibDer, 0, colDibDer, height, paint);
			
			colDibIzq-=2*squareSize/2;
			colDibDer+=2*squareSize/2;
		}	
		
		int despX=45;
		int lastColor = paint.getColor();
		paint.setColor(Color.RED);
		canvas.drawLine(despX, 0, despX, height, paint);
		canvas.drawLine(despX+7, 0, despX+7, height, paint);
		paint.setColor(lastColor);
	}

	public int getSquareSize()
	{
		return squareSize;
	}

	public void setSquareSize(int squareSize)
	{
		this.squareSize = squareSize;
	}

	@Override
	public void onTouch(Lifecicle lf,LifecicleComponents components,long x,long y)
	{
	}
}
